package com.sntk.registermanager.staff.infrastructure.commit.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sntk.registermanager.common.exception.DefinedException;
import com.sntk.registermanager.common.infrastructure.resp.HisOperatorInfo;
import com.sntk.registermanager.common.infrastructure.resp.OperatorInfo;
import com.sntk.registermanager.common.infrastructure.resp.ResponseData;
import com.sntk.registermanager.staff.domain.event.UserAddEvent;
import com.sntk.registermanager.staff.domain.model.User;
import com.sntk.registermanager.staff.infrastructure.commit.UserCommit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @author liuyunxia
 */
@Component("userCommitStaffImpl")
public class UserCommitConvert {
    @Autowired
    private UserCommit userCommit;

    public void addUser(UserAddEvent userAddEvent){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uname", userAddEvent.getUser().getUname());
        jsonObject.put("userName", userAddEvent.getUser().getUserName());
        jsonObject.put("institutionNumber", userAddEvent.getInstitutionNumber());
        jsonObject.put("password", userAddEvent.getPassword());
        jsonObject.put("phoneNumber", userAddEvent.getPhoneNumber());
        jsonObject.put("userType", userAddEvent.getUserType());
        jsonObject.put("remark", userAddEvent.getRemark());
        ResponseData responseData = userCommit.addUser(jsonObject, userAddEvent.getOperatorInfo().getOperator());
        if(!responseData.getCode().equals("200")){
            throw new DefinedException("创建用户失败", "403");
        }
    }

    public User searchUser(String uname){
        ResponseData<String> responseData = userCommit.searchUser(uname);
        if(!responseData.getCode().equals("200")){
            return null;
        }
        if(ObjectUtils.isEmpty(responseData.getData())){
            return null;
        }
        return JSON.parseObject(responseData.getData(), User.class);
    }

}
